#include "stdafx.h"

//#include "MemoryParser.h"
 
using namespace std; 

namespace Melting
{ 

#ifndef __FINALROM
	//-----------------------------------------------------------------------------	
	// Print infos to the console
	//-----------------------------------------------------------------------------	
	void MemoryParser::Print( void ) const
	{	
		LOG("MemoryParser infos :\n");
				
		if( nullptr != mxaBuffer )
		{
			LOG(".parser position : %d\n", GetPosition());
			LOG(".memory chunk size: %d\n", GetMemoryChunkSize());
			LOG(".memory address: %d\n", mxaBuffer);
			LOG(".memory content : \n");
			
			for(ULONG ulPos = 0; ulPos < GetMemoryChunkSize(); ulPos ++ )
			{
				if( 0 == ulPos % 20 )
				{
					LOG("\n");					
				}
				
				LOG("%d ", mxaBuffer[ulPos] );
			}	
			LOG("\n");
		}
		else
		{		
			LOG(". No memory chunk set !!\n");
		}	
	}
	
	//-----------------------------------------------------------------------------		
	// Unit test the methods of this class
	//-----------------------------------------------------------------------------	
	bool MemoryParser::UnitTest( void )
	{
		LOG("::::::::: CLASS : MemoryParser ::::::::::\n");
			
		FLOAT vTest1[ 3 ] = { FLOAT(0.5f), FLOAT(1.5f) , FLOAT(2.5f) }; 
	
		MemoryParser memParser;
		
	
		UTEST_START;

		// --- 0. --------------------------------------------------------------------
		LOG("-- 0.SetMemoryChunk && GetMemoryChunkSize \n");
		{	 						
			memParser.SetMemoryChunk( (UBYTE*)& vTest1, sizeof(vTest1) );
			
			UTEST( sizeof(vTest1) == memParser.GetMemoryChunkSize() );				
		}
		
		// --- 1. --------------------------------------------------------------------
		LOG("-- 1. SetPosition & GetPosition \n");
		{	 						
			memParser.SetPosition( sizeof(FLOAT) );
			
			UTEST( sizeof(FLOAT) == memParser.GetPosition() );
				
			memParser.SetPosition( 0 );
			
			UTEST( 0 == memParser.GetPosition() );				
		}
		
		// --- 2. --------------------------------------------------------------------
		LOG("-- 2. MovePosition \n");
		{	 						
			memParser.SetPosition( 0 );
			memParser.MovePosition( sizeof(FLOAT) );
			
			UTEST( sizeof(FLOAT) == memParser.GetPosition() );
			
			memParser.MovePosition( sizeof(FLOAT) );
			
			UTEST( 2 * sizeof(FLOAT) == memParser.GetPosition() );
			
			memParser.MovePosition( sizeof(FLOAT) );
			
			UTEST( 3 * sizeof(FLOAT) == memParser.GetPosition() );				
		}
		
	 	// --- 3. --------------------------------------------------------------------
		LOG("-- 3.Read \n");
		{
			memParser.SetPosition( 0 );
			
			FLOAT fxVal( 0 );
			
			memParser.Read( fxVal );
			UTEST( vTest1[0] == fxVal );
			
			memParser.Read( fxVal );
			UTEST( vTest1[1] == fxVal );
			
			memParser.Read( fxVal );
			UTEST( vTest1[2] == fxVal );
		}
	
	 	// --- 4. --------------------------------------------------------------------
		LOG("-- 4.ReadArray \n");
		{
			memParser.SetPosition( 0 );
			
			FLOAT aVal[3];
			memParser.ReadArray( 3, aVal );
			UTEST( vTest1[0] == aVal[0] && vTest1[1] == aVal[1] && vTest1[2] == aVal[2] );
		}
		
	 	// --- 5. --------------------------------------------------------------------
		LOG("-- 5.ReadRaw \n");
		{
			memParser.SetPosition( 0 );
			
			FLOAT aVal[3];
			memParser.ReadRaw( 3 * sizeof(FLOAT), aVal );
			UTEST( vTest1[0] == aVal[0] && vTest1[1] == aVal[1] && vTest1[2] == aVal[2] );
		}
		
		
		
 		return UTEST_RESULT;
	}
#endif
}